package com.yaha.controler.remote;

import com.yaha.controler.devices.pojo.MdnsDevice;
import com.yaha.controler.remote.bean.GSensor;
import com.yaha.controler.remote.bean.KeyInfo;
import com.yaha.controler.remote.bean.Mouse;
import com.yaha.controler.remote.bean.MultiTouchInfo;
import com.yaha.controler.remote.protocol.OtORemoteJoystickTask;
import com.yaha.controler.remote.protocol.OtORemoteKeyboardTask;
import com.yaha.controler.remote.protocol.OtORemoteMouseTask;
import com.yaha.controler.remote.protocol.OtORemoteSensorTask;
import com.yaha.controler.remote.protocol.OtORemoteTouchTask;
import com.yaha.controler.utils.MyLog;

public class OtODeviceAdapter {
	private static final String TAG = OtODeviceAdapter.class.getSimpleName();

	private static OtODeviceAdapter gAdapter = null;

	private OtORemoteKeyboardTask mKeyboardTask = null;

	private OtORemoteSensorTask mSensorTask = null;
	private OtORemoteTouchTask mTouchTask = null;
	private OtORemoteMouseTask mMouseTask = null;
	private OtORemoteJoystickTask mJoystickTask = null;

	private OtODeviceAdapter() {
	}

	/**
	 * <功能描述>单例模式实现
	 * 
	 * @return
	 */
	public static OtODeviceAdapter create() {
		if (gAdapter == null)
			gAdapter = new OtODeviceAdapter();
		return gAdapter;
	}

	/**
	 * <功能描述>向选中的设备发送相应的按键
	 * 
	 * @param remote
	 *            远程设备
	 * @param key
	 *            对应键值
	 */
	public void key(MdnsDevice remote, int key) {
		if (mKeyboardTask == null) {
			mKeyboardTask = new OtORemoteKeyboardTask(remote, key);
			Thread thread = new Thread(mKeyboardTask);
			thread.start();
		} else {
			mKeyboardTask.setRemote(remote);
			mKeyboardTask.sendKey(key);
		}
	}

	/**
	 * <功能描述>向选中的设备发送相应的按键和Action
	 * 
	 * @param remote
	 *            远程设备
	 * @param key
	 *            对应键值
	 * @param action
	 *            对应Action
	 */
	public void key(MdnsDevice remote, int key, int action) {
		if (mKeyboardTask == null) {
			mKeyboardTask = new OtORemoteKeyboardTask(remote, key);
			Thread thread = new Thread(mKeyboardTask);
			thread.start();
		} else {
			mKeyboardTask.setRemote(remote);
			mKeyboardTask.sendKeyAction(key, action);
		}
	}

	public void keyDown(MdnsDevice remote, int key) {
		if (mKeyboardTask == null) {
			mKeyboardTask = new OtORemoteKeyboardTask(remote, key, KeyInfo.KEY_EVENT_DOWN);
			Thread thread = new Thread(mKeyboardTask);
			thread.start();
		} else {
			mKeyboardTask.setRemote(remote);
			mKeyboardTask.sendKeyAction(key, KeyInfo.KEY_EVENT_DOWN);
		}
	}

	public void keyUp(MdnsDevice remote, int key) {
		if (mKeyboardTask == null) {
			mKeyboardTask = new OtORemoteKeyboardTask(remote, key, KeyInfo.KEY_EVENT_UP);
			Thread thread = new Thread(mKeyboardTask);
			thread.start();
		} else {
			mKeyboardTask.setRemote(remote);
			mKeyboardTask.sendKeyAction(key, KeyInfo.KEY_EVENT_UP);
		}

	}

	public void mouse(MdnsDevice remote, Mouse mouse) {
		if (mMouseTask == null) {
			mMouseTask = new OtORemoteMouseTask(remote);
			Thread thread = new Thread(mMouseTask);
			thread.start();
		} else {
			mMouseTask.setRemote(remote);
			mMouseTask.sendMouse(mouse);
		}
	}

	public void sensor(MdnsDevice remote, GSensor sensor) {
		if (mSensorTask == null) {
			mSensorTask = new OtORemoteSensorTask(remote, sensor);
			Thread sensorThread = new Thread(mSensorTask);
			sensorThread.start();
		} else {
			mSensorTask.setRemote(remote);
			mSensorTask.sendSensor(sensor);
		}
	}

	public void touch(MdnsDevice remote, MultiTouchInfo touch) {
		MyLog.d(TAG, "[touch] mTouchTask : " + mTouchTask);
		if (mTouchTask == null) {
			mTouchTask = new OtORemoteTouchTask(remote);
			Thread touchTask = new Thread(mTouchTask);
			touchTask.start();
		} else {
			mTouchTask.setRemote(remote);
			mTouchTask.sendTouch(touch);
		}
	}

	public void joystick(MdnsDevice remote, char[] joystickAction) {
		// Log.d(TAG, "[joystick] mJoystickTask : " + mJoystickTask);
		if (null == mJoystickTask) {
			mJoystickTask = new OtORemoteJoystickTask(remote);
			Thread joystickTask = new Thread(mJoystickTask);
			joystickTask.start();
		}

		mJoystickTask.setRemote(remote);
		mJoystickTask.sendJoystickAction(joystickAction);
	}

	public void releaseRes() {
		if (mKeyboardTask != null) {
			mKeyboardTask.release();
			mKeyboardTask = null;
		}

		if (mMouseTask != null) {
			mMouseTask.release();
			mMouseTask = null;
		}

		if (mSensorTask != null) {
			mSensorTask.release();
			mSensorTask = null;
		}

		if (mTouchTask != null) {
			mTouchTask.release();
			mTouchTask = null;
		}

		if (mJoystickTask != null) {
			mJoystickTask.release();
			mJoystickTask = null;
		}

		gAdapter = null;
	}
}
